package com.cssw.fyzb.tps.third.tf56pay.util;

import com.cssw.fyzb.tps.third.tf56pay.common.SortType;
import com.cssw.fyzb.tps.util.StringUtil;

import java.security.MessageDigest;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 * 通用类型工具类
 */
public class SignCommonUtils {

    public static Map<String, String> filterNulAndSort(Map<String, String> map, SortType sortType, boolean filterNull) {
        Map<String, String> sortedMap = new TreeMap<>();
        if (sortType.name().equals(SortType.DESC.name())) {
            sortedMap = new TreeMap<>(new Comparator<String>() {
                @Override
                public int compare(String obj1, String obj2) {
                    // 降序排序
                    return obj2.compareTo(obj1);
                }
            });
        } else if (sortType.name().equals(SortType.ASC.name())) {
            sortedMap = new TreeMap<>(new Comparator<String>() {
                @Override
                public int compare(String obj1, String obj2) {
                    // 升序排序
                    return obj1.compareTo(obj2);
                }
            });
        }
        if (map == null || map.isEmpty()) {
            return sortedMap;
        }
        if (filterNull) {
            //删除值为空的key
            Iterator<String> iter = map.keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next();
                if (null == map.get(key)) {
                    iter.remove();
                }
            }
        }
        sortedMap.putAll(map);
        return sortedMap;
    }

    public static String concatKeyAndValue(Map<String, String> map) {
        StringBuilder result = new StringBuilder();
        for (Entry<String, String> entry : map.entrySet()) {
            result.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        return result.toString();
    }

    public static String concatValue(Map<String, String> map) {
        StringBuilder result = new StringBuilder();
        for (Entry<String, String> entry : map.entrySet()) {
            result.append(entry.getValue());

        }
        return result.toString();
    }

    /**
     * sha1摘要
     * 
     * @param digest
     * @return
     */
    public static String encryptBySHA1(String digest, String charset) {
        if (StringUtil.isBlank(digest)) {
            return null;
        }
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(digest.getBytes(charset));
            byte[] md = mdTemp.digest();
            int j = md.length;
            char[] buf = new char[j * 2];
            int k = 0;
            for (byte byte0 : md) {
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
